using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


using Ultimate.DAL.CollectionClasses;
using Ultimate.DAL;
using Ultimate.DAL.HelperClasses;
using Ultimate.DAL.EntityClasses;
using Ultimate.DAL.FactoryClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

using System.Web.Security;
using System.Web.Mail;

using CommunityServer;
using CommunityServer.Components;

namespace Ultimate
{
	/// <summary>
	/// Summary description for Passwords.
	/// </summary>
	public class Import : System.Web.UI.Page
	{
        
		private void Page_Load(object sender, System.EventArgs e)
		{
			
		}
        protected void BeginImport(object o, System.EventArgs e)
        {

            CreateUserStatus  status;
            string errorMessage="";

            PlayerCollection pc = new PlayerCollection();
            PredicateExpression A = new PredicateExpression();
            A.Add(PredicateFactory.CompareNull(PlayerFieldIndex.UserID));

            pc.GetMulti(A);
            foreach (PlayerEntity pe in pc.Items)
            {
                CommunityServer.Components.User user = new User();
                // try to create the new user account

                user.Username = pe.FirstName + " " + pe.LastName;
                user.Email = pe.Email;
                user.Password = pe.Password;
                user.AccountStatus = UserAccountStatus.ApprovalPending;
                user.IsAnonymous = false;
                try
                {
                    status = Users.Create(user, false);
                }
                catch (CSException exception)
                {
                    status = exception.CreateUserStatus;
                }
                catch (Exception ex)
                {
                    status = MemberRoleProfileProvider.Instance().Members.GetCreateUserStatus(ex);
                }
                try
                {
                    MembershipUser mu2 = Membership.GetUser(pe.FirstName + " " + pe.LastName);
                    pe.UserID = (Guid)mu2.ProviderUserKey;
                    pe.Save();
                }
                catch (Exception ex)
                {
                    errorMessage += "Unable to grab user";
                }
                errorMessage += pe.FirstName + " " + pe.LastName + " <br />\n";

                // Determine if the account was created successfully
                //
                switch (status)
                {

                    // Username is disallowed
                    case CreateUserStatus.DisallowedUsername:
                        errorMessage += CommunityServer.Components.ResourceManager.GetString("CreateNewAccount_CreateUserStatus_DisallowedUsername");
                        
                        break;

                    // Username already exists!
                    case CreateUserStatus.DuplicateUsername:
                       errorMessage +=  CommunityServer.Components.ResourceManager.GetString("CreateNewAccount_CreateUserStatus_DuplicateUsername");
                        
                        break;

                    // Email already exists!
                    case CreateUserStatus.DuplicateEmailAddress:
                       errorMessage += CommunityServer.Components.ResourceManager.GetString("CreateNewAccount_CreateUserStatus_DuplicateEmailAddress");
                        
                        break;

                    case CreateUserStatus.InvalidEmail:
                        errorMessage += ResourceManager.GetString("CreateNewAccount_CreateUserStatus_InvalidEmail");
                        
                        break;

                    case CreateUserStatus.InvalidPassword:
                        errorMessage += ResourceManager.GetString("CreateNewAccount_CreateUserStatus_InvalidPassword");
                        
                        break;

                    case CreateUserStatus.InvalidQuestionAnswer:
                        errorMessage += ResourceManager.GetString("CreateNewAccount_CreateUserStatus_QA");
                        
                        break;

                    case CreateUserStatus.InvalidUserName:
                        errorMessage += ResourceManager.GetString("CreateNewAccount_CreateUserStatus_InvalidUserName");
                        
                        break;

                    // Unknown failure has occurred!
                    case CreateUserStatus.UnknownFailure:
                        errorMessage += CommunityServer.Components.ResourceManager.GetString("CreateNewAccount_CreateUserStatus_UnknownFailure");
                        
                        break;

                }



            }
            Response.Write(errorMessage);
        }
    
		/// <summary>
		/// This can be used to create a random password that can then be emailed to users that forget there passwords
		/// </summary>
		/// <param name="PasswordLength"></param>
		/// <returns></returns>
		public static string CreateRandomPassword(int PasswordLength)
		{
			string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789!@$?";
			Byte[] randomBytes = new Byte[PasswordLength];
			char[] chars = new char[PasswordLength];
			int allowedCharCount = _allowedChars.Length;

			Random randomObj = new Random(DateTime.Now.Millisecond);

			for(int i = 0;i<PasswordLength;i++)
			{
				randomObj.NextBytes(randomBytes);
				chars[i] = _allowedChars[(int)randomBytes[i] % allowedCharCount];
			}

			return new string(chars);
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
